<?php

/**

 * 物流自提服务站首页

 *

 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)

 * @license    http://www.shopwwi.c om

 * @link       交流群号：111731672

 * @since      网店运维提供技术支持 授权请购买shopnc授权

 */

defined('ByShopWWI') or exit('Access Invalid!');

class optionsControl extends BaseChainCenterControl

{

    public function __construct()

    {

        parent::__construct();

        // $model_member = Model('member');

        $sj = Model('store')->getStoreInfoByID($_SESSION['chain_store_id']);

        $model_member = Model('member');

        $member_info = $model_member->getMemberInfo(array('member_id' => $sj['member_id']));

        Tpl::output('member_info', $member_info);

        // $model_member = Model('member');

        // $member_info = $model_member->getMemberInfo(array('member_id'=>$_SESSION['chain_id']));

        // exit(print_r($member_info));

    }

    public function indexOp()

    {

        $model_order = Model('order');

        $condition = array();

        $condition['chain_id'] = $_SESSION['chain_name'];

        $condition['chain_code'] = array('gt', 0);

        // Tpl::output('options_list',$order_list);

        Tpl::output('show_page', $model_order->showpage());

        Tpl::showpage('options.list');

    }

    public function checkmemberOp()

    {

        $name = trim($_GET['name']);

        if (!$name) {

            echo '';

            die;

        }

        /**

         * 转码

         */

        if (strtoupper(CHARSET) == 'GBK') {

            $name = Language::getGBK($name);

        }

        $obj_member = Model('member');

        $member_info = $obj_member->getMemberInfo(array('member_name' => $name));

        if (is_array($member_info) && count($member_info) > 0) {

            if (strtoupper(CHARSET) == 'GBK') {

                $member_info['member_name'] = Language::getUTF8($member_info['member_name']);

            }
            $member_info['member_mobile'] = Language::getUTF8($member_info['member_mobile']);

            echo json_encode(array('id' => $member_info['member_id'], 'name' => $member_info['member_name'],'member_truename' => $member_info['member_truename'], 'mobile' => $member_info['member_mobile'], 'points' => round($member_info['member_points'], 2)));

        } else {

            echo '';

            die;

        }

    }

    public function shoukuanOp()

    {

        // echo $_POST['member_name'];

        $chain_name = $_SESSION['chain_name'];

        //$chin_id = $_SESSION['chain_id'];

        $chain_name = $_SESSION['chain_name'];

        $member_name = $_POST['member_name'];

        $member_je = $_POST['member_amount'];

        $pamount = $member_je;

        $member_sm = $_POST['member_shuming'];

        $member_points = $member_je * 100;

        $xf_date = strtotime(date("Y-m-d H:i:s", time()));

        $model_member = Model('member');

        $member_list = $model_member->where(array('member_name' => $member_name))->Select();

        $member_id = $member_list[0]['member_id'];

        // 查询门店库存积分

        $model_chain = Model('chain');

        //$chain_list = $model_chain -> where(array('chain_id' => $chin_id)) -> Select();

        

		$chain_list = $model_chain->where(array('chain_name' => $chain_name))->Select();

        $store_id = $chain_list[0]['store_id'];//门店ID

		

		//查询门店所属的会员账号

		

		$md_store = Model('store');

		

		$md_store_list = $md_store->where(array('store_id' => $store_id))->Select();

		

		$me_name= $md_store_list[0]['member_name'];//自身会员号

		//print_r($me_name);

		//判断是否是自己购买

		if($me_name==$member_name){

			

			 showDialog('不能购买自己店铺的产品!');

			

			}

		

		

		        $sjinfo = Model('store')->getStoreInfoByID($store_id);

                $sj_member_info = $model_member->getMemberInfo(array('member_id' => $sjinfo['member_id']));

                //商家

                $sj_invite_one = $sj_member_info['invite_one'];

                $sj_invite_two = $sj_member_info['invite_two'];

                $sj_invite_three = $sj_member_info['invite_three'];

                $sj_member_name = $sj_member_info['member_name'];

		

		

        $model_store = Model('store');

        $store_list = $model_store->where(array('store_id' => $store_id))->Select();

        $m_id = $store_list[0]['member_id'];

        $model_ms = Model('member');

        $ms_list = $model_ms->where(array('member_id' => $m_id))->Select();

        $kcpoints = $ms_list[0]['kucunpoints'];

        $m_name = $ms_list[0]['member_name'];

        if ($member_id) {

            $pl_addtime = strtotime(date("Y-m-d"));

            // 添加消费豆

            $sql_xf = "insert into shopwwi_xiaofei(member_id,store_id,member_zh,member_je,member_sm,member_date) values({$member_id},{$chin_id},'{$member_name}',{$member_points},'{$member_sm}',{$xf_date})";

            Model()->execute($sql_xf);

            // 修改会员积分

            Model()->execute('update shopwwi_member set member_points = member_points + ' . $member_points . ' where member_id=' . $member_id);

            // 添加积分日志

            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$member_id},'{$member_name}',{$member_points},{$pl_addtime},'线下门店购物消费-会员宝豆增加','storepoints')";

            Model()->execute($sql_bc);

            //修改库存积分

            $m_points = "-" . $member_points;

            Model()->execute('update shopwwi_member set kucun_points = kucun_points - ' . $member_points . ' where member_id=' . $m_id);

            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$m_id},'{$m_name}',{$m_points},{$pl_addtime},'线下门店购物消费-商家库存积分减少','kucunpoints')";

            Model()->execute($sql_bc);

            //商家的会员账号返16%

            $shiliu = $member_points * 16 / 100;

            Model()->execute('update shopwwi_member set member_points = member_points + ' . $shiliu . ' where member_id=' . $m_id);

            $sql_bc = "insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$m_id},'{$m_name}',{$shiliu},{$pl_addtime},'线下门店购物消费-商家宝豆返还','kucunpoints')";

            Model()->execute($sql_bc);

            //

            $model_member = Model('member');

            $member_list = $model_member->where(array('member_id' => $member_id))->Select();

            // 经验值

            $invite_one = $member_list[0]['invite_one'];

            $invite_two = $member_list[0]['invite_two'];

            $invite_three = $member_list[0]['invite_three'];

            $member_name = $member_list[0]['member_name'];

            $vip = $member_list[0]['member_exppoints'];

            // 当前会员经验值

            if ($invite_one > 0) {

                $member_oneinfo = $model_member->getMemberInfo(array('member_id' => $invite_one));

                $member_onegradeinfo = $model_member->getOneMemberGrade(intval($member_oneinfo['member_exppoints']));

                // 查询所在级别的等级值返回

                //Array

                //(

                //  [level] => 1

                //  [level_name] => V1

                //  [exppoints] => 999

                //  [nxmoney] => 5

                //  [nnxmoney] => 5

                //  [sale_tc] => 3

                //  [nsale_tc] => 1

                //  [shengjifx] => 20

                //  [nshengjifx] => 10

                //  [orderdiscount] => 0

                //)

                //1

                // 上一级会员经验值

                $fy_one = $pamount * $member_onegradeinfo['nxmoney'];



				//

                // 查询上级会员

                $m_name = $member_oneinfo['member_name'];

                $m_id = $member_oneinfo['member_id'];

                $fy_jifen = $member_points * $member_onegradeinfo['nxmoney'];

                // 现金

                // Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$fy_cash.' where member_id='.$m_id);

                // 宝豆

                //Model()->execute('update shopwwi_member set member_points = member_points + ' . $fy_one . ' where member_id=' . $m_id);

                // 宝豆日志

                //$sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的返佣积分','backpoints')";

                

				  // 分红豆

                Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_one . ' where member_id=' . $m_id);

                // 分红豆日志

                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的线下订单一级分红豆返佣','redpoints','". $member_id ."' )";

               

				

				Model()->execute($sql_bc);

            }

            // *******************************************

            if ($invite_two > 0) {

                $member_oneinfo = $model_member->getMemberInfo(array('member_id' => $invite_two));

                $member_onegradeinfo = $model_member->getOneMemberGrade(intval($member_oneinfo['member_exppoints']));

                // 查询所在级别的等级值返回

                //Array

                //(

                //  [level] => 1

                //  [level_name] => V1

                //  [exppoints] => 999

                //  [nxmoney] => 5

                //  [nnxmoney] => 5

                //  [sale_tc] => 3

                //  [nsale_tc] => 1

                //  [shengjifx] => 20

                //  [nshengjifx] => 10

                //  [orderdiscount] => 0

                //)

                //1

                // 上一级会员经验值

                $fy_one = $pamount * $member_onegradeinfo['nnxmoney'];

				

                // 查询上级会员

                $m_name = $member_oneinfo['member_name'];

                $m_id = $member_oneinfo['member_id'];

                $fy_jifen = $member_points * $member_onegradeinfo['nnxmoney'];

                // 现金

                // Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$fy_cash.' where member_id='.$m_id);

                // 宝豆

                //Model()->execute('update shopwwi_member set member_points = member_points + ' . $fy_one . ' where member_id=' . $m_id);

                // 宝豆日志

                //$sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的返佣积分','backpoints')";

                //分红豆 

				 Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_one . ' where member_id=' . $m_id);

                // 分红豆日志

                $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的线下订单二级分红豆返佣','redpoints','". $member_id ."' )";

                

				

				

				Model()->execute($sql_bc);

            }

           //-- if ($invite_three > 0) {

            //--     $member_oneinfo = $model_member->getMemberInfo(array('member_id' => $invite_three));

            //--     $member_onegradeinfo = $model_member->getOneMemberGrade(intval($member_oneinfo['member_exppoints']));

                // 查询所在级别的等级值返回

                //Array

                //(

                //  [level] => 1

                //  [level_name] => V1

                //  [exppoints] => 999

                //  [nxmoney] => 5

                //  [nnxmoney] => 5

                //  [sale_tc] => 3

                //  [nsale_tc] => 1

                //  [shengjifx] => 20

                //  [nshengjifx] => 10

                //  [orderdiscount] => 0

                //)

                //1

                // 上一级会员经验值

              //--   $fy_one = $pamount * $member_onegradeinfo['nsale_tc'];

                // 查询上级会员

               //--  $m_name = $member_oneinfo['member_name'];

               //--  $m_id = $member_oneinfo['member_id'];

              //--   $fy_jifen = $member_points * $member_onegradeinfo['nsale_tc'];

                // 现金

                // Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$fy_cash.' where member_id='.$m_id);

                // 积分

                //Model()->execute('update shopwwi_member set member_points = member_points + ' . $fy_one . ' where member_id=' . $m_id);

                // 积分日志

                //$sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的返佣积分','backpoints')";

                //分红豆 

			 //--	 Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_one . ' where member_id=' . $m_id);

                // 分红豆日志

              //--   $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的线下订单推荐商家一级返佣分红豆','redpoints','". $member_id ."' )";

                 

				

			 //--	Model()->execute($sql_bc);

             //--}

			

			//以下是商家的上级

                if ($sj_invite_one > 0) {

                    $member_oneinfo = $model_member->getMemberInfo(array('member_id' => $sj_invite_one));

                    $member_onegradeinfo = $model_member->getOneMemberGrade(intval($member_oneinfo['member_exppoints']));

                    $yuer = $invite_one + $vip_one + 0.5;

                    //Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$yuer.'  where member_id=33');

                    //$vip_one_one=$md_one_list[0]['invite_one'];//一级分佣

                    //$vip_one_two=$md_one_list[0]['invite_two'];//二级分佣

                    //Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$yuer.'  where member_id=33');

                    //  Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + 0.03  where member_id=33');

                    //***************一级分佣

                    //if($vip_one_one > 0){

                    $fy_one = $pamount * $member_onegradeinfo['sale_tc'];

                    //查询上级会员

                    $m_name = $member_oneinfo['member_name'];

                    $m_id = $member_oneinfo['member_id'];

                    $fy_jifen = $baijifen * $member_onegradeinfo['sale_tc'];

                    $fy_cash = $pamount * $member_onegradeinfo['sale_tc'];

                    //现金

                    //Model() -> execute('update shopwwi_member set available_predeposit = available_predeposit + ' . $fy_cash . ' where member_id=' . $m_id);

                    //积分

                   // Model()->execute('update shopwwi_member set member_points = member_points + ' . $fy_jifen . ' where member_id=' . $m_id);

                    //积分日志

                   // $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $sj_member_name . "的返佣积分','backpoints')";

                       //分红豆

				     Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_one . ' where member_id=' . $m_id);

                    //分红豆日志

                    $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的订单推荐商家一级分红豆返佣','redpoints','". $member_id ."')";

                    

					

					

					Model()->execute($sql_bc);

                    //}

                    //***************

                }

                //*******************************************

                if ($sj_invite_two > 0) {

                    $member_oneinfo = $model_member->getMemberInfo(array('member_id' => $sj_invite_two));

                    $member_onegradeinfo = $model_member->getOneMemberGrade(intval($member_oneinfo['member_exppoints']));

                    //Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$yuer.'  where member_id=33');

                    //$vip_one_one=$md_one_list[0]['invite_one'];//一级分佣

                    //$vip_one_two=$md_one_list[0]['invite_two'];//二级分佣

                    //Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$yuer.'  where member_id=33');

                    //  Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + 0.03  where member_id=33');

                    //***************一级分佣

                    //if($vip_one_one > 0){

                    $fy_one = $pamount * $member_onegradeinfo['nsale_tc'];

                    //查询上级会员

                    $m_name = $member_oneinfo['member_name'];

                    $m_id = $member_oneinfo['member_id'];

                    $fy_jifen = $baijifen * $member_onegradeinfo['nsale_tc'];

                    $fy_cash = $pamount * $member_onegradeinfo['nsale_tc'];

                    //现金

                    //	Model() -> execute('update shopwwi_member set available_predeposit = available_predeposit + ' . $fy_cash . ' where member_id=' . $m_id);

                    //积分

                   // Model()->execute('update shopwwi_member set member_points = member_points + ' . $fy_jifen . ' where member_id=' . $m_id);

                    //积分日志

                   // $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $sj_member_name . "的返佣积分','backpoints')";

                       //分红豆

					Model()->execute('update shopwwi_member set member_red_points = member_red_points + ' . $fy_one . ' where member_id=' . $m_id);

                    //分红豆日志

                    $sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage,pl_upmemberid) values({$m_id},'{$m_name}',{$fy_one},{$pl_addtime},'" . $member_name . "的订单推荐商家二级分红豆返佣','redpoints','". $member_id ."')";

                    

					

					

					Model()->execute($sql_bc);

                }

			

			

			

            echo "<script>alert('收银成功!');history.go(-1)</script>";

            // ********************************************

        } else {

            echo "<script>alert('账号不存在');history.go(-1)</script>";

        }

    }

    /**

     * 提货验证

     */

    public function pickup_parcelOp()

    {

        if (chksubmit()) {

            $order_id = intval($_POST['order_id']);

            $pickup_code = intval($_POST['pickup_code']);

            if ($order_id <= 0 || $pickup_code <= 0) {

                showDialog(L('wrong_argument'));

            }

            $model_order = Model('order');

            $order_info = $model_order->getOrderInfo(array('order_id' => $order_id, 'chain_code' => $pickup_code));

            if (empty($order_info)) {

                showDialog('提货码错误');

            }

            $logic_order = Logic('order');

            $if_allow = $model_order->getOrderOperateState('chain_receive', $order_info);

            if (!$if_allow) {

                showDialog('无权操作');

            }

            $result = $logic_order->changeOrderStateReceive($order_info, 'chain', '自提门店', '商品已被买家自提，门店更改订单为完成状态');

            if ($result['state']) {

                showDialog('提货成功', 'reload', 'succ', 'DialogManager.close("pickup_parcel")');

            } else {

                showDialog($result['msg']);

            }

        } else {

            $order_model = Model('order');

            $condition['order_id'] = intval($_GET['order_id']);

            $condition['chain_id'] = $_SESSION['chain_id'];

            $order_info = $order_model->getOrderInfo($condition, array('order_common', 'order_goods'));

            foreach ($order_info['extend_order_goods'] as $k => $value) {

                $value['image_url'] = cthumb($value['goods_image'], 60, $value['store_id']);

                $value['goods_type_cn'] = orderGoodsType($value['goods_type']);

                $value['goods_url'] = urlShop('goods', 'index', array('goods_id' => $value['goods_id']));

                $order_info['extend_order_goods'][$k] = $value;

            }

            usort($order_info['extend_order_goods'], function ($a, $b) {

                if ($a['goods_type'] == $b['goods_type']) {

                    return 0;

                }

                return $a['goods_type'] > $b['goods_type'] ? 1 : -1;

            });

            Tpl::output('order_info', $order_info);

        }

        Tpl::showpage('order.pickup_parcel', 'null_layout');

    }

}